package com.ahui.ydd.controller;


import com.ahui.ydd.common.R;
import com.ahui.ydd.dto.SysRoleDto;
import com.ahui.ydd.entity.SysRole;
import com.ahui.ydd.entity.SysUser;
import com.ahui.ydd.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys_role")
public class SysRoleController {
    
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/list") //角色列表分页page
    public R<Page> list(Integer page,Integer limit,String name ){
        log.info("page={},pageSize={}",page,limit,name);

        //构造分页构造器
        Page pageInfo = new Page(page,limit);

        //构造条件构造器
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),SysRole::getName,name);

        //添加查询条件
        queryWrapper.orderByDesc(SysRole::getUpdateDate);
        //执行查询
        sysRoleService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }
    //
    @PostMapping("/update")  //角色修改
    public R<String> update(@RequestBody SysRoleDto sysRoleDto){
        log.info("修改角色信息:{}",sysRoleDto.toString());

        sysRoleService.updateById(sysRoleDto);
        return R.success("");
    }


    @GetMapping("/rowInfo")  //角色详情getById
    public R<SysRole> rowInfo(String id){
        log.info("根据id查询角色详情");
        SysRole sysRole = sysRoleService.getById(id);
        if(sysRole != null){
            return R.success(sysRole);
        }
        return R.error("没有查询到对应员工信息");

    }

    @GetMapping("/allData")   //角色列表-全部getAll
    public R<List<SysRole>> allData(){
        List<SysRole> list = sysRoleService.list();
/*        for (SysRole sysRole : list) {
            System.out.println(sysRole.toString());
        }*/
        return R.success(list);


    }
    @PostMapping("/add")  //角色添加
    public R<String> add(@RequestBody SysRoleDto sysRoleDto){
        log.info("新增角色，角色信息:{}",sysRoleDto.toString());

        sysRoleService.save(sysRoleDto);
        return R.success("");

    }

    @GetMapping("/del")  //角色删除
    public R<String> del(String id){
        log.info("删除管理员，id为：{}",id);
        //将删除字段 set del_flag = 1
        LambdaUpdateWrapper<SysRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysRole::getId,id).set(SysRole::getDelFlag,1);
        sysRoleService.update(wrapper);

        return R.success("");
    }

}
